package com.woniuxy.ams.controller;

import com.woniuxy.ams.entity.Clause;
import com.woniuxy.ams.entity.ResultEntity;
import com.woniuxy.ams.service.ClauseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/dayAssessClause")
public class ClauseController {
    /**
     * 自动注入 clauseService  制度表
     */
    ClauseService clauseService;
    @Autowired(required = false)
    public void setClauseService(ClauseService clauseService) {
        this.clauseService = clauseService;
    }

    /**
     * 新增制度条款
     * @param clause
     * @return
     */
    @RequestMapping("/insert")
    public ResultEntity<Integer> insertClause(@RequestBody Clause clause){
        int code = clauseService.insert(clause);
        if (code!=0){
            return new ResultEntity<>(200,"新增成功！",code);
        }else {
            return new ResultEntity<>(400,"新增失败！",code);
        }
    }

    /**
     * 删除制度条款
     * @param clause
     * @return
     */
    @RequestMapping("/delete")
    public ResultEntity<Integer> deleteClause(@RequestBody Clause clause){
        int code = clauseService.delete(clause);
        if (code!=0){
            return new ResultEntity<>(200,"删除成功！",code);
        }else {
            return new ResultEntity<>(400,"删除失败！",code);
        }
    }

    /**
     * 查询所有
     */
    @RequestMapping("/findClause")
    public ResultEntity<List<Clause>> findAllClause(){
        List<Clause> allClause = clauseService.findAllClause();
        for (Clause clause : allClause) {
            System.out.println(clause);
        }
        return new ResultEntity<>(200,"查询成功!",allClause);
    }

    /**
     * 修改 动态修改 按条件修改
     */
    @RequestMapping("/update")
    public ResultEntity<Integer> updateClause(@RequestBody Clause clause){
        int code =  clauseService.updateClause(clause);
        if (code!=0){
            return new ResultEntity<>(200,"修改成功！",code);
        }else {
            return new ResultEntity<>(400,"修改失败！",code);
        }
    }
}
